<?php

namespace Home\Biz;

class CommonBiz {
	/**
	 * 获取不重复唯一键
	 */
	public static function getNewSeq($type) {
		M ( 'seq', '', 'DB_system' )->execute ( "INSERT INTO `seq` (`name`) VALUES ('$type')" );
		$seq = M ( 'seq', '', 'DB_system' )->max ( 'id' );
		$seq = ( int ) $seq;
		return $seq;
	}
	
	/**
	 * 获取不重复订单相关唯一键
	 */
	public static function getNewOrderSeq($type) {
		M ( 'seq', '', 'DB_system' )->execute ( "INSERT INTO `order_seq` (`name`) VALUES ('$type')" );
		$seq = M ( 'order_seq', '', 'DB_system' )->max ( 'id' );
		$seq = ( int ) $seq;
		return $seq;
	}
	public static function getAllProvince() {
		$lst_en = M ( 'province', '', 'DB_system' )->select ();
		return $lst_en;
	}
	public static function getCityListByProvinceId($province_id) {
		
		$lst_en = M ( 'city', '', 'DB_system' )->where(array('province_id' => $province_id))->select ();
		return $lst_en;
	}
	public static function getDistrictByCityId($city_id) {
		
		$lst_en = M ( 'district', '', 'DB_system' )->where(array('city_id' => $city_id))->select ();
		return $lst_en;
	}
	
	public static function getGetParamsStr(){
		$get_str_array = array();
		$all_get_keys = array_keys($_GET);
		
		foreach($all_get_keys as $key){
			if(!is_numeric($key)){
				array_push($get_str_array, $key."=".$_GET[$key]);
			}
		}
		$get_str = implode('&', $get_str_array);
		return $get_str;
	}


	public static function getPostParamsStr(){
		$get_str_array = array();
		$all_get_keys = array_keys(I('post.'));
		foreach($all_get_keys as $key){
			if(!is_numeric($key)){
				array_push($get_str_array, $key."=".$_POST[$key]);
			}
		}
		$get_str = implode('&', $get_str_array);
		return $get_str;
	}
	
	
	public static function getParamsStr(){ 
		$str_array 	= array(); 
		$get_str 	= CommonBiz::getGetParamsStr();
		$post_str 	= CommonBiz::getPostParamsStr();
		array_push($str_array, $get_str);
		array_push($str_array, $post_str);
		
		$str 		= implode('&', $str_array);
		return trim($str, '&');
	}
	
	public static function getRequestParams(){
		return array_merge($_POST, $_GET);
	}

	/**
	 * 判断请求是否来自于手机设备
	 */
	public static function isFromMobile() {
		// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
		if (isset ( $_SERVER ['HTTP_X_WAP_PROFILE'] ))
			return true;
			
			// 此条摘自TPM智能切换模板引擎，适合TPM开发
		if (isset ( $_SERVER ['HTTP_CLIENT'] ) && 'PhoneClient' == $_SERVER ['HTTP_CLIENT'])
			return true;
			// 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
		if (isset ( $_SERVER ['HTTP_VIA'] ))
			// 找不到为flase,否则为true
			return stristr ( $_SERVER ['HTTP_VIA'], 'wap' ) ? true : false;
			// 判断手机发送的客户端标志,兼容性有待提高
		if (isset ( $_SERVER ['HTTP_USER_AGENT'] )) {
			$clientkeywords = array (
					'nokia',
					'sony',
					'ericsson',
					'mot',
					'samsung',
					'htc',
					'sgh',
					'lg',
					'sharp',
					'sie-',
					'philips',
					'panasonic',
					'alcatel',
					'lenovo',
					'iphone',
					'ipod',
					'blackberry',
					'meizu',
					'android',
					'netfront',
					'symbian',
					'ucweb',
					'windowsce',
					'palm',
					'operamini',
					'operamobi',
					'openwave',
					'nexusone',
					'cldc',
					'midp',
					'wap',
					'mobile' 
			);
			// 从HTTP_USER_AGENT中查找手机浏览器的关键字
			if (preg_match ( "/(" . implode ( '|', $clientkeywords ) . ")/i", strtolower ( $_SERVER ['HTTP_USER_AGENT'] ) )) {
				return true;
			}
		}
		// 协议法，因为有可能不准确，放到最后判断
		if (isset ( $_SERVER ['HTTP_ACCEPT'] )) {
			// 如果只支持wml并且不支持html那一定是移动设备
			// 如果支持wml和html但是wml在html之前则是移动设备
			if ((strpos ( $_SERVER ['HTTP_ACCEPT'], 'vnd.wap.wml' ) !== false) && (strpos ( $_SERVER ['HTTP_ACCEPT'], 'text/html' ) === false || (strpos ( $_SERVER ['HTTP_ACCEPT'], 'vnd.wap.wml' ) < strpos ( $_SERVER ['HTTP_ACCEPT'], 'text/html' )))) {
				return true;
			}
		}
		return false;
	}
	/**
	 * Description: 计算坐标距离(结果返回米)
	 * @version 2016年9月28日 下午7:20:45 
	 * @author lin.yujiao@whaley.cn
	 * @param unknown $lat1
	 * @param unknown $lng1
	 * @param unknown $lat2
	 * @param unknown $lng2
	 * @param string $miles
	 */
	public static function getDistance($lat1, $lng1, $lat2, $lng2){
		//圆周率π在角度里为180°,Math.PI/180就为1°
		$pi80 = M_PI / 180;
		//WGS84坐标系 长半轴
		//千米
		//$earth_radius = 6372.797;
		//米
		$earth_radius = 6378137 ;
		$lat1 *= $pi80;
		$lat2 *= $pi80;
		/* 	$lng1 *= $pi80;
			$lng2 *= $pi80;  */
	
		$dlat = $lat1 - $lat2;
		$dlng = ($lng1 - $lng2) * $pi80;
	
		$slat = sin($dlat/2);
		$slng = sin($dlng/2);
		$distance =  2 * $earth_radius * asin(sqrt($slat * $slat + cos($lat1)* cos($lat2) * $slng * $slng));
		return $distance;
	}
}